package com.yorick.common.config.netty;

import com.yorick.netty.EchoServer;
import com.yorick.netty.WebSocketServer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;

@Component
public class EchoServerConfig {
    @Value("${netty.startWithSpring}")
    private boolean autoStart;
    @Value("${netty.startWebSocketWithSpring}")
    private boolean autoStartWebSocket;
    @Resource
    EchoServer echoServer;
    @Resource
    WebSocketServer webSocketServer;

        @PostConstruct
        public void setAutoStart(){
            if (autoStart) {
                echoServer.start();
            }
            if (autoStartWebSocket) {
                webSocketServer.start();
            }
        }

        @PreDestroy
        public void end(){
            if(autoStart) {
                echoServer.stop();
            }
            if (autoStartWebSocket) {
                webSocketServer.stop();
            }
        }
}
